package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.ListModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;

import java.awt.Cursor;
import java.awt.ComponentOrientation;
import javax.swing.ListSelectionModel;
import java.awt.Rectangle;
import javax.swing.JSpinner;

import types.DEG_TYPE;
import types.GENDER_TYPE;
import types.DEPT_TYPES;
import types.PROF_TYPE;
import user.User;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

public class PersonalInformationFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane, panel_0, panel_18, panel_17, panel_16, panel_15, panel_13, panel_11,
					panel_9, panel_7, panel_5, panel_3, panel_1, panel_19, panel_2, panel_4, panel_6, 
					panel_8, panel_10, panel_12, panel_14;
	private JLabel nameLabel;
	private JTextField nameField;
	private JLabel dobLabel;
	private JFormattedTextField dobField;
	private JLabel genderLabel;
	private JList genderList;
	private JLabel phoneLabel;
	private JTextField phoneField;
	private JLabel addrLabel;
	private JTextField addrField;
	private JLabel permAddrLabel;
	private JTextField permAddrField;
	private JLabel emailLabel;
	private JTextField emailField;
	private JButton submitBttn;
	private JRadioButton tutYesRdBttn, tutNoRdBttn;
	private ButtonGroup tutBttnGroup;
	private JLabel tutorLabel;
	private JLabel prefTutLabel;
	private JList prefTutList;
	private Component prefTutBttn;
	private JScrollPane prefTutScrollPane;
	private JLabel chosenTutLabel;
	private JList chosenTutList;
	private JScrollPane chosenTutScrollPane;
	private JButton chosenTutBttn;
	private JLabel majorLabel;
	private JLabel lblDegree;
	private JSpinner majorSpinner;
	private JSpinner degreeSpinner;
	private JLabel deptLabel;
	private JLabel courseLabel;
	private JLabel sectionLabel;
	private JLabel positionLabel;
	private JTextField resIntField;
	private JList deptList;
	private JScrollPane deptScrollPane;
	private JList courseList;
	private JScrollPane courseScrollPane;
	private JList sectionList;
	private JScrollPane sectionScrollPane;
	private JList positionList;
	private JScrollPane positionScrollPane;
	private JLabel resIntLabel;
	
	private User currUser;
	
///////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
///////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * Create the frame.
	 */
	public PersonalInformationFrame(User currUser) {
		this.currUser = currUser;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new GridLayout(10, 2, 0, 0));
		setContentPane(contentPane);
		
		panel_0 = new JPanel();
		contentPane.add(panel_0);
		
		nameLabel = new JLabel("Name: ");
		panel_0.add(nameLabel);
		
		nameField = new JTextField();
		panel_0.add(nameField);
		nameField.setColumns(10);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1);
		
		dobLabel = new JLabel("DOB (YYYY-MM-DD): ");
		panel_1.add(dobLabel);
		
		dobField = new JFormattedTextField(new SimpleDateFormat("yyyy-MM-dd"));
		panel_1.add(dobField);
		dobField.setColumns(10);
		
		panel_2 = new JPanel();
		contentPane.add(panel_2);
		
		genderLabel = new JLabel("Gender: ");
		panel_2.add(genderLabel);
		
		genderList = new JList();
		genderList.setModel(new DefaultComboBoxModel(GENDER_TYPE.values()));
		panel_2.add(genderList);
		
		panel_3 = new JPanel();
		contentPane.add(panel_3);
		
		phoneLabel = new JLabel("Phone: ");
		panel_3.add(phoneLabel);
		
		phoneField = new JTextField();
		panel_3.add(phoneField);
		phoneField.setColumns(10);
		
		panel_4 = new JPanel();
		contentPane.add(panel_4);
		
		addrLabel = new JLabel("Address:");
		panel_4.add(addrLabel);
		
		addrField = new JTextField();
		panel_4.add(addrField);
		addrField.setColumns(10);
		
		panel_5 = new JPanel();
		contentPane.add(panel_5);
		
		permAddrLabel = new JLabel("Perm. Address:");
		panel_5.add(permAddrLabel);
		
		permAddrField = new JTextField();
		panel_5.add(permAddrField);
		permAddrField.setColumns(7);
		
		panel_6 = new JPanel();
		contentPane.add(panel_6);
		
		emailLabel = new JLabel("Email: ");
		panel_6.add(emailLabel);
		
		emailField = new JTextField();
		panel_6.add(emailField);
		emailField.setColumns(10);
		
		panel_7 = new JPanel();
		contentPane.add(panel_7);

		panel_8 = new JPanel();
		contentPane.add(panel_8);
		
		panel_9 = new JPanel();
		contentPane.add(panel_9);

		panel_10 = new JPanel();
		contentPane.add(panel_10);

		panel_11 = new JPanel();
		contentPane.add(panel_11);
		
		panel_12 = new JPanel();
		contentPane.add(panel_12);
		
		panel_13 = new JPanel();
		contentPane.add(panel_13);
		
		panel_14 = new JPanel();
		contentPane.add(panel_14);
		
		panel_15 = new JPanel();
		contentPane.add(panel_15);
		
		panel_16 = new JPanel();
		contentPane.add(panel_16);
		
		panel_17 = new JPanel();
		contentPane.add(panel_17);
		
		panel_18 = new JPanel();
		contentPane.add(panel_18);
		
		panel_19 = new JPanel();
		contentPane.add(panel_19);
		
		submitBttn = new JButton("Submit");
		panel_19.add(submitBttn);
		submitBttn.addActionListener(insertInfoListener);
		
		if (currUser.isStudent())
			setupStudent();
		else
			setupInstructor();
		
	}
	//----------------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////////////////
// BUTTON LISTENERS
///////////////////////////////////////////////////////////////////////////////////////////////////////

	private ActionListener insertInfoListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (isFilledOut()) {
				System.out.println("Form filled out completely");
				currUser.dumpData();

				if (currUser.isStudent()) 
					currUser.updateStudUser(majorSpinner.getValue().toString(), degreeSpinner.getValue().toString());
				else
					currUser.updateInstUser(deptList.getSelectedValue().toString(), positionList.getSelectedValue().toString(), resIntField.getText());
				
				currUser.updateRegUser(nameField.getText(), emailField.getText(), dobField.getText(), 
						addrField.getText(), permAddrField.getText(),((GENDER_TYPE)genderList.getSelectedValue()).toChar(), 
						phoneField.getText());
				
				HomePageFrame hFrame = new HomePageFrame(currUser);
				hFrame.setVisible(true);
				PersonalInformationFrame.this.dispose();
			}
			else {
				new JOptionPane().showMessageDialog(PersonalInformationFrame.this, "Fill out Form Completely");
			}
			
		}
	};
	




		
///////////////////////////////////////////////////////////////////////////////////////////////////////
// METHODS
///////////////////////////////////////////////////////////////////////////////////////////////////////
	public void setupStudent() { 
		
		tutorLabel = new JLabel("Willing to Tutor: ");
		panel_7.add(tutorLabel);
		
		tutYesRdBttn = new JRadioButton("Yes");
		panel_7.add(tutYesRdBttn);
		
		tutNoRdBttn = new JRadioButton("No");
		panel_7.add(tutNoRdBttn);
		tutNoRdBttn.setSelected(true);
		
		tutBttnGroup = new ButtonGroup();
		tutBttnGroup.add(tutNoRdBttn);
		tutBttnGroup.add(tutYesRdBttn);
		
		prefTutLabel = new JLabel("Select preferred courses to tutor");
		panel_8.add(prefTutLabel);
		
		String[] prefTutStrArr = {"Hello0", "Hello1", "Hello2", "4"};
		//TODO populate list with courses available to be tutored
		
		prefTutList = new JList(prefTutStrArr);
		prefTutList.setBounds(new Rectangle(0, 0, 0, 10));
		prefTutList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		prefTutList.setSelectedIndex(1);
		prefTutList.setDoubleBuffered(true);
		prefTutList.setVisibleRowCount(2);
		
		prefTutScrollPane = new JScrollPane(prefTutList);
		panel_10.add(prefTutScrollPane);
		
		prefTutBttn = new JButton("Add");
		panel_10.add(prefTutBttn);
		
		chosenTutLabel = new JLabel("Preferred Courses to tutor");
		panel_9.add(chosenTutLabel);
		
		String[] chosenTutStrArr = {"Hello0", "Hello1", "Hello2", "4"};
		//TODO populate list with courses available to be tutored
		
		chosenTutList = new JList(chosenTutStrArr);
		chosenTutList.setBounds(new Rectangle(0, 0, 0, 10));
		chosenTutList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		chosenTutList.setSelectedIndex(1);
		chosenTutList.setDoubleBuffered(true);
		chosenTutList.setVisibleRowCount(2);
		
		chosenTutScrollPane = new JScrollPane(chosenTutList);
		panel_11.add(chosenTutScrollPane);
		
		chosenTutBttn = new JButton("Remove");
		panel_11.add(chosenTutBttn);
		
		majorLabel = new JLabel("Major: ");
		panel_12.add(majorLabel);
		
		majorSpinner = new JSpinner(new SpinnerListModel(DEPT_TYPES.values()));
		majorSpinner.setMinimumSize(new Dimension(250, 28));
		majorSpinner.setPreferredSize(new Dimension(270, 28));
		panel_12.add(majorSpinner);
		
		lblDegree = new JLabel("Degree: ");
		panel_13.add(lblDegree);
		
		degreeSpinner = new JSpinner(new SpinnerListModel(DEG_TYPE.values()));
		degreeSpinner.setPreferredSize(new Dimension(60, 28));
		degreeSpinner.setMinimumSize(new Dimension(60, 28));
		panel_13.add(degreeSpinner);
	}
	//----------------------------------------------------------------------------------------------
	
	public void setupInstructor() {

		deptLabel = new JLabel("Dept:");
		panel_7.add(deptLabel);
		
		deptList = new JList(new DefaultComboBoxModel(DEPT_TYPES.values()));
		deptList.setBounds(new Rectangle(0, 0, 0, 10));
		deptList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		deptList.setSelectedIndex(1);
		deptList.setDoubleBuffered(true);
		deptList.setVisibleRowCount(2);
		deptList.setSelectedIndex(0);
		
		deptScrollPane = new JScrollPane(deptList);
		panel_7.add(deptScrollPane);
		
		courseLabel = new JLabel("Course: ");
		panel_8.add(courseLabel);
		
		courseList = new JList(new DefaultComboBoxModel());
		courseList.setBounds(new Rectangle(0, 0, 0, 10));
		courseList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		courseList.setSelectedIndex(1);
		courseList.setDoubleBuffered(true);
		courseList.setVisibleRowCount(2);
		courseList.setSelectedIndex(0);
		
		courseScrollPane = new JScrollPane(courseList);
		panel_8.add(courseScrollPane);
		
		sectionLabel = new JLabel("Section: ");
		panel_9.add(sectionLabel);
		
		sectionList = new JList(new DefaultComboBoxModel());
		sectionList.setBounds(new Rectangle(0, 0, 0, 10));
		sectionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		sectionList.setSelectedIndex(1);
		sectionList.setDoubleBuffered(true);
		sectionList.setVisibleRowCount(2);
		sectionList.setSelectedIndex(0);
		
		sectionScrollPane = new JScrollPane(sectionList);
		panel_9.add(sectionScrollPane);

		positionLabel = new JLabel("Position: ");
		panel_10.add(positionLabel);
		
		positionList = new JList(new DefaultComboBoxModel(PROF_TYPE.values()));
		positionList.setBounds(new Rectangle(0, 0, 0, 10));
		positionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		positionList.setSelectedIndex(1);
		positionList.setDoubleBuffered(true);
		positionList.setVisibleRowCount(2);
		positionList.setSelectedIndex(0);
		
		positionScrollPane = new JScrollPane(positionList);
		panel_10.add(positionScrollPane);
		
		resIntLabel = new JLabel("Research Interests: ");
		panel_11.add(resIntLabel);
		
		resIntField = new JTextField();
		panel_11.add(resIntField);
		resIntField.setColumns(12);
	}
	//----------------------------------------------------------------------------------------------

	
	public void setupStudentEducation() {
		
	}
	//----------------------------------------------------------------------------------------------


	/**
	 * Checks to see if every required item is filled out on the form
	 * @return boolean representing completeness of the form
	 */
	private boolean isFilledOut() {
		if (nameField.getText().isEmpty()) {
			return false;
		}
		else if (dobField.getText().isEmpty()){
			return false;
		}
		else if (genderList.isSelectionEmpty()) {
			return false;
		}
		else if (addrField.getText().isEmpty()) {
			return false;
		}
		else if (permAddrField.getText().isEmpty()) {
			return false;
		}
		else if (phoneField.getText().isEmpty()) {
			return false;
		}
		else if (emailField.getText().isEmpty()) {
			return false;
		}
		else if (currUser.isStudent() && !isStudentFilledOut()) {
			return false;
		}
		else if (currUser.isInstructor() && !isInstructorFilledOut()) {
			return false;
		}
		else {
			return true;
		}
	}
	//----------------------------------------------------------------------------------------------
	
	/**
	 * Check to see if student form is filled out
	 */
	private boolean isStudentFilledOut() {
		
		return true;
	}
	//----------------------------------------------------------------------------------------------

	/**
	 * Check to see if instructor form is filled out
	 */
	private boolean isInstructorFilledOut() {
		if (resIntField.getText().isEmpty()) {
			return false;
		}
		else
			return true;
	}
	//----------------------------------------------------------------------------------------------
	
	/**
	 * populates the data for the user
	 */
	private void populateForm() {
		
	}
	//----------------------------------------------------------------------------------------------

	/**
	 * populates student specific data
	 */
	private void populateStudentForm() {
		
	}
	//----------------------------------------------------------------------------------------------

	/**
	 * populates inst specific data
	 */
	private void populateInstructorForm() {
		
	}
	//----------------------------------------------------------------------------------------------



	
}
